package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.vo.UserRecharge;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;

public class UserRechargeJdbc extends BaseJdbc {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String INSERT_USER_RECHAREG = "INSERT INTO auction.user_recharge (userId, bidding, giftBidding, amount, ip, creationTime, transTime, STATUS, orderNo, tradeNo, orderSubject, orderBody, buyerEmail)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";


    /**
     * 保存用户充值记录
     *
     * @param userRecharge
     */
    public void save(UserRecharge userRecharge) {
        Connection conn = this.getConnection();
        Object[] pram = {userRecharge.getUserId(), userRecharge.getBidding(), userRecharge.getGiftBidding(), userRecharge.getAmount(), userRecharge.getIp(), userRecharge.getCreationTime(),
                userRecharge.getTransTime(), userRecharge.getStatus(), userRecharge.getOrderNo(), userRecharge.getTradeNo(), userRecharge.getOrderSubject(), userRecharge.getOrderBody(), userRecharge.getBuyerEmail()};
        try {
            this.execute(conn, INSERT_USER_RECHAREG, pram);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
    }


    /**
     * 充值查询个数
     *
     * @return
     */
    public int getRows(int userId) {
        Connection conn = this.getConnection();
        String sql = "select count(id) from user_recharge where userId=?";
        int count = 0;
        try {
            count = this.getRows(conn, sql,userId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
        return count;
    }

    /**
     * 充值查询列表
     *
     * @param pageSize
     * @param currentPage
     * @return
     */
    public List<UserRecharge> findWithPage(int currentPage, int pageSize,int userId) {
        List<UserRecharge> userRechargeList = new ArrayList<UserRecharge>();
        Connection conn = this.getConnection();
        String sql = "select ur.* from user_recharge ur where ur.userId=? order by ur.creationTime desc limit ?,?";
        try {
            int start = (currentPage - 1) * pageSize;
            userRechargeList = this.query(conn, sql, new BeanListHandler<UserRecharge>(UserRecharge.class),userId, start, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
        return userRechargeList;
    }


    /**
     * 获得
     */
    public UserRecharge getByOrderNo(String orderNo) {
        UserRecharge userRecharge = null;
        Connection conn = this.getConnection();
        String sql = "select * from user_recharge ur where ur.orderNo = " + orderNo;
        try {
            userRecharge = this.query(conn, sql, new BeanHandler<UserRecharge>(UserRecharge.class));

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close(conn);
        }
        return userRecharge;
    }
}
